package com.qd.panda.record;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.record.UserBillRecordVO;
import com.qd.common.panda.domain.entity.record.UserBillRecordsDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.billrecord.BillRecordsHolderService;
import com.qd.panda.service.billrecord.UserBillRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * APP_开票记录
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/userBillRecords/")
@Slf4j
public class UserBillRecordsController {
    private final UserBillRecordsService userBillRecordsService;
    private final BillRecordsHolderService billRecordsHolderService;

    /**
     * 添加开票记录
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/save")
    public ApiResultWrapper<Object> save(@Validated({DefaultGroup.class}) @RequestBody UserBillRecordVO dto) {
        billRecordsHolderService.saveUserBillRecord(dto);
        return ApiResult.success("添加成功");
    }

    /**
     * 获取开票记录
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserBillRecordsDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return userBillRecordsService.getByIdDto(id);
    }

    /**
     * 开票记录列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserBillRecordsDTO> page(UserBillRecordsDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userBillRecordsService.page(PageUtil.buildPage(dto), dto);
    }
}